package com.server.llh.llhserverwork.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.bean.SerTransField;
import com.server.llh.llhserverwork.bean.SerTransWapBean;
import com.server.llh.llhserverwork.utils.TimeUtils;

import java.util.List;


public class HealthTaskAdapter  extends BaseAdapter{

	List<SerTransWapBean> listData;
	public List<SerTransWapBean>  getListData() {
		return listData;
	}

	public void setListData(List<SerTransWapBean> listData) {
		this.listData = listData;
	}
	private Context context;

	public HealthTaskAdapter(Context context, List<SerTransWapBean> listData) {
		this.listData = listData;
		this.context = context;
	}

	@Override
	public int getCount() {
		return listData == null ? 0 : listData.size();
	}

	@Override
	public Object getItem(int arg0) {
		return listData == null ? null : listData.get(arg0);
	}
	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = new ViewHolder();
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.tahsk_finisha, null);
			holder.server_name = (TextView) convertView.findViewById(R.id.client_name);
			holder.client_age = (TextView) convertView.findViewById(R.id.client_age);
			holder.server_time = (TextView) convertView.findViewById(R.id.server_time);
			holder.order_time = (TextView) convertView.findViewById(R.id.order_time);
			holder.client_num = (TextView) convertView.findViewById(R.id.client_num);
			holder.client_server = (TextView) convertView.findViewById(R.id.client_server);
			holder.client_address = (TextView) convertView.findViewById(R.id.client_address);
			holder.subscribe_server_order = (TextView) convertView.findViewById(R.id.subscribe_server_order);
			holder.subscribe_Revoke = (TextView) convertView.findViewById(R.id.subscribe_Revoke);
			holder.content_view = (ListView) convertView.findViewById(R.id.content_view);
			holder.ll_value = (LinearLayout) convertView.findViewById(R.id.ll_value);
			HealthValueAdapter adapter=new HealthValueAdapter();
			holder.content_view.setAdapter(adapter);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		SerTransWapBean taskBean=listData.get(position);

		if (taskBean.getIsHasField()==1) {
			HealthValueAdapter otherAdapter = null;
			if (holder.content_view.getAdapter() != null) {
				otherAdapter = (HealthValueAdapter) holder.content_view.getAdapter();
			} else {
				otherAdapter = new HealthValueAdapter();
			}
			otherAdapter.setSerTransField(taskBean.getFieldList());
			otherAdapter.notifyDataSetChanged();
		}else{
			holder.ll_value.setVisibility(View.GONE);
		}
		holder.order_time.setText(TimeUtils.dateToString(taskBean.getAddTime()));
		holder.server_name.setText(taskBean.getCustomerName());
		holder.client_age.setText("");

//		holder.client_address.setText("￥ "+taskBean.getPrice()+"元");
		if (taskBean.getRemark()!=null) {
			holder.client_address.setText("描述:"+taskBean.getRemark());
		}else{
			holder.client_address.setVisibility(View.GONE);
		}
		holder.client_num.setText(taskBean.getCustomerPhone());
		holder.client_server.setText(taskBean.getServiceName());
		String startTime= TimeUtils.dateToString(taskBean
				.getSerStartTime());
		String endTimeStr= TimeUtils.dateToString(taskBean.getSerEndTime());
		holder.server_time.setText(startTime.substring(5, startTime.length()-3)
				+ "--"
				+endTimeStr.substring(5,endTimeStr.length()-3) );
		return convertView;
	}

	class ViewHolder {
		TextView server_name, server_time, server_finish, order_time,
		client_server, client_age, client_address, client_num,
		subscribe_Revoke, subscribe_server_order, textcreate,
		client_server_time,unitName;
		ListView content_view;
		ImageView llh_image;
		TextView te_name;
		TextView te_values;
		LinearLayout ll_value;
	}

	class HealthValueAdapter extends BaseAdapter {
		List<SerTransField> serTransField;

		public List<SerTransField> getSerTransField() {
			return serTransField;
		}

		public void setSerTransField(List<SerTransField> serTransField) {
			this.serTransField = serTransField;
		}

		public HealthValueAdapter() {
		}

		HealthValueAdapter(List<SerTransField> serTransField) {
			this.serTransField = serTransField;
		}

		@Override
		public int getCount() {
			if (serTransField == null || serTransField.size() == 0) {
				return 0;
			}
			return serTransField.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			if (convertView == null) {
				ViewHolder h1 = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.add_value_item, null);
				h1.te_name = (TextView) convertView
						.findViewById(R.id.textname);
				h1.te_values = (TextView) convertView
						.findViewById(R.id.et_values);
				h1.unitName = (TextView) convertView
						.findViewById(R.id.unitName);
				convertView.setTag(h1);
			}
			final ViewHolder h = (ViewHolder) convertView.getTag();
			SerTransField serTranValue = serTransField.get(position);
			h.te_name.setText(serTranValue.getFieldName());
			h.te_values.setText(serTranValue.getFieldValue() == null ? ""
					: serTranValue.getFieldValue());
			h.te_values.setTag(serTranValue);
			h.unitName.setText(serTranValue.getFieldUnit());
			return convertView;
		}
	}

}
